<?php

// demo订单控制器

namespace app\demo\controller;

use think\Controller;

class Ys extends Controller
{

    public function index()
    {
        $all_merchant = db('payment_merchant_config')->where(['status' => 1])->select();
        $this->assign('all_merchant', $all_merchant);

        $payment_type = pay_config('payment_type');
        $this->assign('payment_type', $payment_type);
        return $this->fetch();
    }

    /**
     * 创建订单接口:只允许创建付款订单
     */
    public function create()
    {

        $rule = [
            'cash' => 'require|float|length:1,10',
            'token' => 'require',
            'type' => 'require|length:1,20',
        ];

        $result = $this->validate($this->request->param(), $rule);
        if (true !== $result) {
            // 验证失败 输出错误信息
            return $this->error($result);
        }

        if (session('__token__') != $this->request->param('token') || session('__token__') == '') {
            return $this->error('token错误');
        }

        $url = request()->domain() . '/api.php/order/create/';                 // 接口请求地址	

        $data['version'] = '1.0';                                              // 版本号
        $data['merchant_id'] = 87654321;                                       // 商户号
        $data['merchant_order_id'] = date('YmdHis') . rand(100000, 999999);    // 商户订单号
        $data['app_id'] = 0;                                                   // 应用id
        $data['cash'] = $this->request->param('cash');                         // 金额（元） 
        $data['type'] = $this->request->param('type');                         // 支付类型 wechat_h5 alipay_h5 wangyin alipay_scan_code wechat_scan_code
        $data['trade_type'] = 'consumption';                                   // 交易类型
        $data['body'] = '商品名称';                                             // 商品名称
        $data['name'] = '0';                                                   // 姓名
        $data['id_card'] = '0';                                                // 身份证号
        $data['mobile'] = '0';                                                 // 手机号
        $data['start_time'] = time();                                          // 订单开始时间:时间戳
        $data['return_url'] = 'https://www.baidu.com';                         // 支付成功服务器返回地址
        $data['notify_url'] = request()->domain() . '/success.html';           // 支付成功页面通知地址
        $data['ip'] = request()->ip();                                         // IP地址
        $data['user_id'] = 0;                                                  // 用户ID
        $data['channel_id'] = 0;                                               // 渠道ID
        $data['format'] = 'json';                                              // 返回格式
        $data['sign_type'] = 'md5';                                            // 签名类型
        $data['business_type'] = 'B2C';                                        // 业务类型
        $data['bank_code'] = "ICBC";                                           // 银行编号
        $data['extra'] = '';                                                   // 商户自定义备注（原值返回）
        $data['random_string'] = date('YmdHis') . rand(100000, 999999);        // 随机字符串

        $key = db('payment_merchant_config')->where(['id' => $data['merchant_id']])->value('key');
        $data['sign'] = md5('merchant_id=' . $data['merchant_id'] . '&merchant_order_id=' . $data['merchant_order_id'] . '&cash=' . $data['cash'] . '&type=' . $data['type'] . '&return_url=' . $data['return_url'] . '&notify_url=' . $data['notify_url'] . '&ip=' . $data['ip'] . '&start_time=' . $data['start_time'] . '&random_string=' . $data['random_string'] . '&key=' . $key);

        $resdata = go_curl($url, $data, 'post', 10);
        $resarray = json_decode($resdata, true);

        if ($resarray['code'] == 10000) {
            header("Location: {$resarray['pay_url']}");
            exit;
        } else {
            return $this->error('错误说明：' . $resarray['msg']);
        }
    }
}
